/**
 * 注册用户
 */
function register() {

    let code = $("#code").val();
    let password = $("#password").val();
    let rePassword = $("#rePassword").val();
    let phone = $("#phone").val();

    /**
     * 用户编号输入格式验证
     * @type {*|DomElement|jQuery|HTMLElement}
     */
    let promptUser = $("#check-username");
    let regUsername = /^[a-zA-Z]\w{1,15}$/;
    if (regUsername.test(code)) {
        promptUser.html("用户编码格式正确");
        promptUser.css("color", "green");
    } else {
        promptUser.html("用户编码格式错误");
        promptUser.css("color", "red");
        return;
    }
    /**
     * 密码格式验证
     * @type {*|DomElement|jQuery|HTMLElement}
     */
    let promptPassword = $("#check-password");
    let regPassword = /^\w{6,15}$/;
    if (regPassword.test(password)) {
        promptPassword.html("密码格式正确");
        promptPassword.css("color", "green");
    } else {
        promptPassword.html("密码格式错误");
        promptPassword.css("color", "red");
        return;
    }

    /**
     * 密码一致验证
     * @type {*|DomElement|jQuery|HTMLElement}
     */
    let checkRePassword = $("#check-rePassword");
    if (password !== rePassword) {
        checkRePassword.html("两次密码输入不一致");
        checkRePassword.css("color", "red");
        return;
    } else {
        checkRePassword.html("密码输入一致");
        checkRePassword.css("color", "green");
    }
    /**
     * 电话号码验证
     * @type {*|DomElement|jQuery|HTMLElement}
     */
    let promptPhone = $("#check-phone");
    let regPhone = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (regPhone.test(phone)) {
        promptPhone.html("手机格式正确");
        promptPhone.css("color", "green");
    } else {
        promptPhone.html("手机格式错误");
        promptPhone.css("color", "red");
        return;
    }

    let data = {
        code: code,
        password: password,
        phone: phone,
    };

    $.ajax({
            url: 'http://localhost:20000/api/auth/user/register' ,
            data: JSON.stringify(data),
            type: 'post',
            async: false,
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                if (data.code === 200) {
                    alert("注册成功！");
                    window.location.href = "/login.html";
                } else if (data.code === 500) {
                    alert("该用户已被注册！");
                } else {
                    alert("注册失败");
                }
            }
        }
    );
}

/**
 * 跳转登录
 */
function toLogin() {
    window.location.href = "/login.html";
}

